import javax.swing.text.AsyncBoxView;
import java.util.Arrays;
import java.util.Comparator;

class Child {  //implements Comparable<Child>
    int age;
    String name;

    public Child(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Child{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

//    @Override
//    public int compareTo(Child c) {
//       return c.age- this.age;
//    }

}

class AgeCmp implements Comparator<Child> {
    @Override
    public int compare(Child o1, Child o2) {
        return (int)(o1.age - o2.age);
    }
}

class StringCmp implements Comparator <Child> {
//    @Override
//    public int compare(Child o1, Child o2) {
//        return o1.name.length() - o2.name.length();
//    }
    @Override
    public int compare(Child o1, Child o2) {
    return o1.name.compareTo(o2.name);
    //比较首字母的
}
}



public class Interface {
    public static void main(String[] args) {
        Child[] childs = new Child[3];
        childs[0] = new Child(20,"Matty");
        childs[1] = new Child(19,"Lyh");
        childs[2] = new Child(21,"Lisa");
        System.out.println(Arrays.toString(childs));
//        AgeCmp ac = new AgeCmp();
//        Arrays.sort(childs,ac);
        StringCmp sc = new StringCmp();
        Arrays.sort(childs,sc);
        System.out.println(Arrays.toString(childs));
    }

    public static void main1(String[] args) {
        Child[] childs = new Child[3];
        childs[0] = new Child(20,"Matty");
        childs[1] = new Child(19,"Lyh");
        childs[2] = new Child(21,"Lisa");
        System.out.println(Arrays.toString(childs));
        Arrays.sort(childs);
        System.out.println(Arrays.toString(childs));
    }
}


